package com.youyicloud.service;

import com.alibaba.fastjson.JSONObject;
import com.youyicloud.entity.WechatInfo;
import com.youyicloud.repository.WechatRepository;
import com.youyicloud.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WechatService {
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private WechatRepository wechatRepository;
	
	
	public WechatInfo findNewWechat(String appId,String appSecret) {
		WechatInfo wechatInfo = wechatRepository.findByAppIdAndAppSecret(appId, appSecret);
		return wechatInfo;
	}
	
	public void updateWechatInfo() {
		List<WechatInfo> wechatInfos = wechatRepository.findAll();
		for (int i = 0; i < wechatInfos.size(); i++) {
			WechatInfo wechatInfo = wechatInfos.get(i);
			String accessTokenValue = getAccessToken(wechatInfo.getAppId(), wechatInfo.getAppSecret());
			String jsapiTicketValue= getJsapiTicket(accessTokenValue);
			wechatInfo.setAccessTokenValue(accessTokenValue);
			wechatInfo.setJsapiTicketValue(jsapiTicketValue);
			wechatInfo.setUpdateTime(DateUtils.getNowDate());
			wechatRepository.saveAndFlush(wechatInfo);
		}
	}

	public String getAccessToken(String appId,String appSecret) {
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appId}&secret={appSecret}";
		Map<String,Object> urlVariables = new HashMap<String,Object>();
		urlVariables.put("appId", appId);
		urlVariables.put("appSecret", appSecret);
		ResponseEntity<String> responseEntity =restTemplate.getForEntity(url, String.class, urlVariables);
		JSONObject obj = JSONObject.parseObject(responseEntity.getBody());
		String access_token = obj.getString("access_token");
		return access_token;
	}

	public String getCode(String appId,String redirectUri,String scope,String state) {
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={appId}&redirect_uri={redirectUri}&responseType=code&scope={scope}&state={state}#wechat_redirect";
		Map<String,Object> urlVariables = new HashMap<String,Object>();
		urlVariables.put("appId", appId);
		urlVariables.put("redirectUri", redirectUri);
		urlVariables.put("scope", scope);
		urlVariables.put("state", state);
		ResponseEntity<String> responseEntity =restTemplate.getForEntity(url, String.class, urlVariables);
		JSONObject obj = JSONObject.parseObject(responseEntity.getBody());
		String access_token = obj.getString("access_token");
		return access_token;
	}

	public String getUserAccessToken(String appId,String secret,String grantType,String code) {
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={appId}&secret={secret}&code={code}&grant_type={grantType}";
		Map<String,Object> urlVariables = new HashMap<String,Object>();
		urlVariables.put("appId", appId);
		urlVariables.put("secret", secret);
		urlVariables.put("grantType", grantType);
		urlVariables.put("code", code);
		ResponseEntity<String> responseEntity =restTemplate.getForEntity(url, String.class, urlVariables);
		JSONObject obj = JSONObject.parseObject(responseEntity.getBody());
		String access_token = obj.getString("access_token");
		return access_token;
	}

	public String getUserInfoByOpenIdAndAccessToken(String openId,String accessToken) {
		String url = "https://api.weixin.qq.com/sns/userinfo?access_token={accessToken}&openid={openId}&lang=zh_CN";
		Map<String,Object> urlVariables = new HashMap<String,Object>();
		urlVariables.put("accessToken", accessToken);
		urlVariables.put("openId", openId);
		ResponseEntity<String> responseEntity =restTemplate.getForEntity(url, String.class, urlVariables);
		return responseEntity.getBody();
	}
	
	public String getJsapiTicket(String accessToken) {
		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
		ResponseEntity<String> responseEntity =restTemplate.getForEntity(url, String.class);
		log.info("jsapi_ticket:{}",responseEntity.getBody());
		JSONObject obj = JSONObject.parseObject(responseEntity.getBody());
        String jsapi_ticket = obj.getString("ticket");
		return jsapi_ticket;
	}
}
